package com.hcb.tonguetiphealth.manager;

import com.hcb.tonguetiphealth.model.User;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;

public class UserManager {
	private DBManager db;
	public UserManager(Activity ctx) {
		db = DBManager.getInstance(ctx);
	}
	/**
	 * 保存用户
	 * @param user
	 * @return
	 */
	public boolean saveUser(User user) {
		ContentValues values = new ContentValues();
		values.put("username", user.getUserName());
		values.put("password", user.getPassWord());
		values.put("logintime", System.currentTimeMillis());
		values.put("rememb", user.getRememb());
		values.put("autologin", user.getAutoLogin());
		
		User temp = getUser();
		if (null == temp) {
			return (db.insert(DBManager.table_user, values)) > 0;
		} else {
			return (db.update(DBManager.table_user, temp.getId()+"", values));
		}
	}
	
	/**
	 * 获取用户
	 * @return
	 */
	public User getUser() {
		User user = null;
		//Cursor mCursor = db.query("user", null, null, null, null, null, null, null);
		Cursor mCursor = db.getCursor(DBManager.table_user);
		if (mCursor.moveToFirst()) {
			user = new User();
			user.setId(mCursor.getInt(0));
			user.setUserName(mCursor.getString(1));
			user.setPassWord(mCursor.getString(2));
			user.setLoginTime(mCursor.getLong(3));
			user.setRememb(mCursor.getInt(4));
			user.setAutoLogin(mCursor.getInt(5));
			
			mCursor.close();
		}
		
		return user;
	}
}
